package com.sunfeng.chap2;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Author : 孙峰
 * @Description:
 * @Date : 2021/10/8  15:26
 */
public class Test2_3 {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(new File("d:\\b.txt"));
        FileChannel fileChannel = fileInputStream.getChannel();
        fileChannel.position(2);
        ByteBuffer byteBuffer = ByteBuffer.allocate(5);
        byteBuffer.position(3);
        // 向byteBuffer读入cd
        fileChannel.read(byteBuffer);

        byte[] array = byteBuffer.array();

        for (int i = 0; i < array.length; i++) {
            if (array[i] == 0) {
                System.out.println("空格");
            } else {
                System.out.println((char) array[i]);
            }
        }
        fileChannel.close();
        fileInputStream.close();
    }
}
